package org.loofer.ext.utils;

import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.ApplicationInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build.VERSION;

public final class ShortcutUtils {
    private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
    private static final String ACTION_UNINSTALL_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";

    private ShortcutUtils() {
    }

    public static boolean installShortcut(Context context, int flags) {
        ApplicationInfo info = context.getApplicationInfo();
        if (info == null) {
            return false;
        }
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(flags);
        return installShortcut(context, intent, info.labelRes, info.icon);
    }

    public static boolean installShortcut(Context context, Intent shortcutIntent) {
        ApplicationInfo info = context.getApplicationInfo();
        return info != null && installShortcut(context, shortcutIntent, info.labelRes, info.icon);
    }

    public static boolean installShortcut(Context context, Intent shortcutIntent, int titleRes, int iconRes) {
        if (hasShortcut(context, titleRes)) {
            return false;
        }
        Intent intent = generateShortcutIntent(context, shortcutIntent, titleRes, iconRes);
        intent.setAction(ACTION_INSTALL_SHORTCUT);
        context.sendBroadcast(intent, null);
        return true;
    }

    public static void uninstallShortcut(Context context, int flags) {
        ApplicationInfo info = context.getApplicationInfo();
        if (info != null) {
            Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            intent.addFlags(flags);
            uninstallShortcut(context, intent, info.labelRes, info.icon);
        }
    }

    public static void uninstallShortcut(Context context, Intent shortcutIntent) {
        ApplicationInfo info = context.getApplicationInfo();
        if (info != null) {
            uninstallShortcut(context, shortcutIntent, info.labelRes, info.icon);
        }
    }

    public static void uninstallShortcut(Context context, Intent shortcutIntent, int titleRes, int iconRes) {
        Intent intent = generateShortcutIntent(context, shortcutIntent, titleRes, iconRes);
        intent.setAction(ACTION_UNINSTALL_SHORTCUT);
        context.sendBroadcast(intent, null);
    }

    public static boolean hasShortcut(Context context, int titleRes) {
        String url;
        if (VERSION.SDK_INT >= 8) {
            url = "content://com.android.launcher2.settings/favorites?notify=true";
        } else {
            url = "content://com.android.launcher.settings/favorites?notify=true";
        }
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(Uri.parse(url), new String[]{"title"}, "title=?", new String[]{context.getString(titleRes)}, null);
            if (cursor == null || cursor.getCount() <= 0) {
                if (cursor != null) {
                    cursor.close();
                }
                return false;
            }
            if (cursor != null) {
                cursor.close();
            }
            return true;
        } catch (Throwable th) {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    private static Intent generateShortcutIntent(Context context, Intent shortcutIntent, int titleRes, int iconRes) {
        Intent intent = new Intent();
        intent.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
        intent.putExtra("android.intent.extra.shortcut.NAME", context.getString(titleRes));
        intent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", ShortcutIconResource.fromContext(context, iconRes));
        intent.putExtra("duplicate", false);
        return intent;
    }
}
